﻿'========================================================================
'功能:人員管理
'作者:shihyu
'日期: 
'========================================================================
Imports System.Data
Imports System.IO
Partial Class WebForm_Manage_EmployeeMaintain
    Inherits BasePage


    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            hiUID.Value = UID
            hiDBName.Value = dbName
            If Not ChkProgramRight(1) Then '查詢
                btnQuery.Visible = False
            End If

            If Not ChkProgramRight(2) Then '新增
                btnInsert.Visible = False
            End If

            If Not ChkProgramRight(3) Then '修改
                gvEMP.Columns(0).Visible = False
            End If

            Me.BindSex()
            Me.BindStatus()
            Me.BindJob()
            Me.BindOrg()
            Me.BindCompany()
        End If
      
    End Sub


    Protected Sub gvEMP_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles gvEMP.PageIndexChanging
        gvEMP.SelectedIndex = -1
        gvEMP.EditIndex = -1
        Me.gvEMP.PageIndex = e.NewPageIndex
        Me.BindGV()
    End Sub

    Protected Sub gvEMP_RowCancelingEdit(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCancelEditEventArgs) Handles gvEMP.RowCancelingEdit
        gvEMP.SelectedIndex = -1
        gvEMP.EditIndex = -1
    End Sub
    Protected Sub btnEdit_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        hiMode.Value = "Edit" '編輯

        txtemp_no2.Text = CType(sender, Button).CommandArgument.ToString()
        lbMode.Text = GetLocalResourceObject("EditMode")
        txtemp_no2.ReadOnly = True
        '取資料 
        Me.GetData()
        mpInsert.Show()
    End Sub
    Protected Sub btnInsert_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnInsert.Click
        lbMode.Text = GetLocalResourceObject("InsMode")
        txtemp_no2.ReadOnly = False
        hiMode.Value = "Ins"
        '清空畫面
        Me.ClearData()
        mpInsert.Show()
    End Sub

    Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
        Dim bbl As WorkFlow.BBL.EmployeeBBL = New WorkFlow.BBL.EmployeeBBL()

      
        Try
            DateTime.Parse(txtcome_date.Text)

        Catch ex As Exception
            ShowMessageForAjax(btnSave, GetLocalResourceObject("ComeDateErr"))
            mpInsert.Show()
            Exit Sub

        End Try
        Try
            If txtquit_date.Text <> "" Then
                DateTime.Parse(txtquit_date.Text)

            End If

        Catch ex As Exception
            mpInsert.Show()
            ShowMessageForAjax(btnSave, GetLocalResourceObject("quit_dateErr"))
            mpInsert.Show()
            Exit Sub

        End Try
        Try

            DateTime.Parse(txtbirthday.Text)


        Catch ex As Exception
            mpInsert.Show()
            ShowMessageForAjax(btnSave, GetLocalResourceObject("BirthdayErr"))
            Exit Sub

        End Try
        If ddlCompany.SelectedValue = "請選擇" Then
            mpInsert.Show()
            ShowMessageForAjax(btnSave, GetLocalResourceObject("ERRcompany"))
            Exit Sub
        End If

        If ddljob_id.SelectedValue = "請選擇" Then
            mpInsert.Show()
            ShowMessageForAjax(btnSave, GetLocalResourceObject("ERRJob"))
            Exit Sub
        End If
        Dim cryObj As Creasys.Utility.DesEncrypt = New Creasys.Utility.DesEncrypt()
        Dim pass As String = cryObj.EncryptString(txtPassword.Text.Trim, "12345678")
        If hiMode.Value = "Ins" Then
            If bbl.EMPIsExist(Me.txtemp_no2.Text.Trim(), hiDBName.Value) Then
                mpInsert.Show()
                ShowMessageForAjax(btnSave, GetLocalResourceObject("EMPExist"))
                Exit Sub
            ElseIf Me.txtemp_no2.Text.Trim() = "" Then
                mpInsert.Show()
                ShowMessageForAjax(btnSave, GetLocalResourceObject("EMPEmpty"))
                Exit Sub
            End If
            bbl.InsertEMP(Me.txtemp_no2.Text.Trim, Me.txtemp_name2.Text.Trim, Me.txtemp_name_eng2.Text.Trim, Me.txtext_no.Text.Trim, Me.txtbirthday.Text.Trim, Me.ddlorg_id.SelectedValue, Me.txtLoginID.Text.Trim, Me.txtEmail.Text.Trim, Me.ddlStatus.SelectedValue, Me.txtcome_date.Text.Trim, Me.ddlCompany.SelectedValue, Me.txtquit_date.Text.Trim, Me.ddljob_id.SelectedValue, Me.ddlsex.SelectedValue, hiUID.Value, pass, dbName)

        Else
            bbl.UpdateEMP(Me.txtemp_no2.Text.Trim, Me.txtemp_name2.Text.Trim, Me.txtemp_name_eng2.Text.Trim, Me.txtext_no.Text.Trim, Me.txtbirthday.Text.Trim, Me.ddlorg_id.SelectedValue, Me.txtLoginID.Text.Trim, Me.txtEmail.Text.Trim, Me.ddlStatus.SelectedValue, Me.txtcome_date.Text.Trim, Me.ddlCompany.SelectedValue, Me.txtquit_date.Text.Trim, Me.ddljob_id.SelectedValue, Me.ddlsex.SelectedValue, hiUID.Value, pass, dbName)
        End If
    End Sub
    Private Sub ClearData()
        Me.txtbirthday.Text = ""
        Me.txtcome_date.Text = ""
        Me.txtemp_name2.Text = ""
        Me.txtemp_name_eng2.Text = ""
        Me.txtemp_no2.Text = ""
        Me.txtLoginID.Text = ""
        Me.txtquit_date.Text = ""
        Me.txtEmail.Text = ""
        Me.ddlCompany.SelectedIndex = -1
        Me.ddljob_id.SelectedIndex = -1
        Me.ddlorg_id.SelectedIndex = -1
        Me.ddlsex.SelectedIndex = -1
        Me.ddlStatus.SelectedIndex = -1
        txtPassword.Text = ""
        lblCrtDate2.Text = ""
        lblCrtName2.Text = ""
        lblModiDate2.Text = ""
        lblModiName2.Text = ""
        lblModiUID.Text = ""
        lblCrtUID.Text = ""
    End Sub
    Private Sub GetData()
        Dim dt As DataTable
        Dim bbl As WorkFlow.BBL.EmployeeBBL = New WorkFlow.BBL.EmployeeBBL()
        dt = bbl.GetEMP(Me.txtemp_no2.Text.Trim(), "", "", hiDBName.Value)
        If dt.Rows.Count = 0 Then
            ClearData()
            Exit Sub
        End If
        Me.txtbirthday.Text = IIf(Not TypeOf (dt.Rows(0)("birthday")) Is DBNull, DateTime.Parse(dt.Rows(0)("birthday")).ToString("yyyy/MM/dd"), "")
        Me.txtcome_date.Text = IIf(Not TypeOf (dt.Rows(0)("come_date")) Is DBNull, DateTime.Parse(dt.Rows(0)("come_date")).ToString("yyyy/MM/dd"), "")
        Me.txtemp_name2.Text = dt.Rows(0)("emp_name").ToString()
        Me.txtemp_name_eng2.Text = dt.Rows(0)("emp_name_eng").ToString()
        Me.txtemp_no2.Text = dt.Rows(0)("emp_no").ToString()
        Me.txtLoginID.Text = dt.Rows(0)("LogonID").ToString()
        Try
            Me.txtquit_date.Text = DateTime.Parse(dt.Rows(0)("quit_date").ToString).ToString("yyyy/MM/dd")
        Catch ex As Exception
            Me.txtquit_date.Text = ""
        End Try

        Me.txtEmail.Text = dt.Rows(0)("e_mail").ToString()
        Me.ddlCompany.SelectedValue = dt.Rows(0)("company").ToString()
        Me.ddljob_id.SelectedValue = dt.Rows(0)("job_id").ToString()
        Me.ddlorg_id.SelectedValue = dt.Rows(0)("org_id").ToString()
        Me.ddlsex.SelectedValue = dt.Rows(0)("sex").ToString()
        Me.ddlStatus.SelectedValue = dt.Rows(0)("status").ToString()
        lblCrtName2.Text = dt.Rows(0)("CrtName").ToString
        lblModiName2.Text = dt.Rows(0)("ModiName").ToString
        lblModiUID.Text = dt.Rows(0)("ModiUID").ToString
        lblCrtUID.Text = dt.Rows(0)("CrtUID").ToString

        Dim cryObj As Creasys.Utility.DesEncrypt = New Creasys.Utility.DesEncrypt()
        txtPassword.Text = cryObj.DecryptString(dt.Rows(0)("Password").ToString, "12345678")
        Try
            lblCrtDate2.Text = DateTime.Parse(dt.Rows(0)("CrtDate").ToString).ToString("yyyy/MM/dd HH:mm:ss")
        Catch ex As Exception
            lblCrtDate2.Text = ""
        End Try

        Try

            lblModiDate2.Text = DateTime.Parse(dt.Rows(0)("ModiDate").ToString).ToString("yyyy/MM/dd HH:mm:ss")
        Catch ex As Exception
            lblModiDate2.Text = ""
        End Try


    End Sub

    Protected Sub btnQuery_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnQuery.Click

        Me.BindGV()
    End Sub


    Protected Sub gvEMP_RowEditing(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewEditEventArgs) Handles gvEMP.RowEditing
        gvEMP.EditIndex = e.NewEditIndex
        Me.BindGV()
    End Sub
    Private Sub BindGV()

        Dim bbl As WorkFlow.BBL.EmployeeBBL = New WorkFlow.BBL.EmployeeBBL()
        Me.gvEMP.DataSource = bbl.GetEMP(Me.txtemp_no.Text.Trim(), Me.txtemp_name.Text.Trim(), Me.txtemp_name_eng.Text.Trim(), hiDBName.Value)
        Me.gvEMP.DataBind()
    End Sub
    Private Sub BindStatus()
        Dim bbl As WorkFlow.BBL.EmployeeBBL = New WorkFlow.BBL.EmployeeBBL()
        Select Case (GetUserLanguage().ToLower)
            Case "en-us"
                Me.ddlStatus.DataTextField = "EngText"
            Case "zh-cn"
                Me.ddlStatus.DataTextField = "ChnText"
            Case Else
                Me.ddlStatus.DataTextField = "Text"
        End Select
        Me.ddlStatus.DataValueField = "Value"
        Me.ddlStatus.DataSource = bbl.GetCodeMapping("emp_data", "status", hiDBName.Value)
        Me.ddlStatus.DataBind()
        ' Me.ddlStatus.Items.Insert(0, "請選擇")

    End Sub
    Private Sub BindSex()
        Dim bbl As WorkFlow.BBL.EmployeeBBL = New WorkFlow.BBL.EmployeeBBL()
        Select Case (GetUserLanguage().ToLower)
            Case "en-us"
                Me.ddlsex.DataTextField = "EngText"
            Case "zh-cn"
                Me.ddlsex.DataTextField = "ChnText"
            Case Else
                Me.ddlsex.DataTextField = "Text"
        End Select
        Me.ddlsex.DataValueField = "Value"

        Me.ddlsex.DataSource = bbl.GetCodeMapping("emp_data", "sex", hiDBName.Value)
        Me.ddlsex.DataBind()
        ' Me.ddlsex.Items.Insert(0, "請選擇")
    End Sub
    Private Sub BindOrg()
        Dim bbl As WorkFlow.BBL.EmployeeBBL = New WorkFlow.BBL.EmployeeBBL()
        Select Case (GetUserLanguage().ToLower)
            Case "en-us"
                Me.ddlorg_id.DataTextField = "OrgEngName"
            Case "zh-cn"
                Me.ddlorg_id.DataTextField = "OrgChnName"
            Case Else
                Me.ddlorg_id.DataTextField = "OrgName"
        End Select
        Me.ddlorg_id.DataValueField = "OrgID"
        Me.ddlorg_id.DataSource = bbl.GetOrg(hiDBName.Value)
        Me.ddlorg_id.DataBind()
        ' Me.ddlorg_id.Items.Insert(0, "請選擇")
    End Sub
    Private Sub BindCompany()
        Dim bbl As WorkFlow.BBL.EmployeeBBL = New WorkFlow.BBL.EmployeeBBL()
        Select Case (GetUserLanguage().ToLower)
            Case "en-us"
                Me.ddlCompany.DataTextField = "CompanyEngName"
            Case "zh-cn"
                Me.ddlCompany.DataTextField = "CompanyChnName"
            Case Else
                Me.ddlCompany.DataTextField = "CompanyName"
        End Select

        Me.ddlCompany.DataValueField = "CompanyID"
        Me.ddlCompany.DataSource = bbl.GetCompany(hiDBName.Value)
        Me.ddlCompany.DataBind()
        '  Me.ddlCompany.Items.Insert(0, "請選擇")
    End Sub
    Private Sub BindJob()
        Dim bbl As WorkFlow.BBL.EmployeeBBL = New WorkFlow.BBL.EmployeeBBL()
        Select Case (GetUserLanguage().ToLower)
            Case "en-us"
                Me.ddljob_id.DataTextField = "JobEngName"
            Case "zh-cn"
                Me.ddljob_id.DataTextField = "JobChnName"
            Case Else
                Me.ddljob_id.DataTextField = "JobName"
        End Select
        Me.ddljob_id.DataValueField = "JobID"
        Me.ddljob_id.DataSource = bbl.GetJobData(hiDBName.Value)
        Me.ddljob_id.DataBind()
        'Me.ddljob_id.Items.Insert(0, "請選擇")
    End Sub
End Class
